/*
 * Created on 2005-9-5
 *
 */
package com.yl.cap.analyst.data;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * @author ZhangQi
 *  
 */
public class DataImporter
{

    public static void main(String[] args) throws Exception
    {
        if (args.length < 2)
        {
            System.out
                    .println("argumens not match!You must input 2 arguments at least.");
            return;
        }
        String strIniFile = args.length==2 ? "db.properties" : args[2];
        
        Properties iniFile = new Properties();
        InputStream iFile = new FileInputStream(strIniFile);
        iniFile.load(iFile);

        String name = iniFile.getProperty("name");
        String pass = iniFile.getProperty("password");
        String sUrl = iniFile.getProperty("url");
        String driver = iniFile.getProperty("driver");

        StringBuffer url = new StringBuffer();
        url.append(sUrl);
        if (sUrl.indexOf("?") != -1)
        {
            url.append("&user=");
        }
        else
        {
            url.append("?user=");    
        }
        url.append(name);
        url.append("&");
        url.append("password=");
        url.append(pass);

        StringBuffer tmpSql = new StringBuffer();
        List values = null;
        List sqlList = new ArrayList();
        RandomAccessFile dataFile = new RandomAccessFile(args[0], "r");
        byte[] content = new byte[(int) dataFile.length()];
        dataFile.read(content);
        String con = new String(content);
        dataFile.close();
        String lineStr = null;
        boolean isFirst = true;
        StringTokenizer conTok = new StringTokenizer(con, "\n\r");
        int id = 1;
        
        System.out.println("please wait...");
        
        while (conTok.hasMoreTokens())
        {
            lineStr = conTok.nextToken();
            if (isFirst)
            {
                tmpSql.append("INSERT INTO ");
                tmpSql.append(args[1]);
                tmpSql.append(" values(");
                tmpSql.append(id++);
                tmpSql.append(",");
            }
            StringTokenizer dataTok = new StringTokenizer(lineStr, ";");
            int i = 0;
            values = new ArrayList();
            while (dataTok.hasMoreTokens())
            {
                String tmpData = dataTok.nextToken();
                if (tmpData.indexOf("\"") != -1)
                {
                    //tmpData = new String(tmpData.substring(1,tmpData.length() - 1).getBytes("utf8"));
                    tmpData = tmpData.substring(1,tmpData.length() - 1);
                    values.add(tmpData);
                }
                else
                {
                    values.add(new BigDecimal(tmpData));
                }
                if (i++ != 0)
                {
                    if (isFirst)
                    {
                        tmpSql.append(",");
                    }
                }
                if (isFirst)
                {
                    tmpSql.append("?");
                }
            }
            if (isFirst)
            {
                tmpSql.append(");");
                isFirst = false;
            }
            sqlList.add(values);
        }

        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url.toString());
        PreparedStatement s = conn.prepareStatement(tmpSql.toString());
        for (int i = 0; i < sqlList.size(); i++)
        {
            List sqlArg = (List) sqlList.get(i);
            for (int j = 0; j < sqlArg.size(); j++)
            {
                if (sqlArg.get(j) instanceof String)
                {
                    s.setString(j+1,(String)sqlArg.get(j));    
                }
                else if (sqlArg.get(j) instanceof BigDecimal)
                {
                    s.setBigDecimal(j+1,(BigDecimal)sqlArg.get(j));
                }
            }
            s.executeUpdate();
        }
        
        System.out.println("option complete!");
    }
}
